#ifndef CONTROL_H
#define CONTROL_H

#include <QThread>
#include <QString>
#include <sys/types.h>
#include <pthread.h>
#include <semaphore.h>

#include "waterTank.h"

#define OK  0
#define NOT 1

#define WT_HEIGHT 5 //altura de ambas as caixas d'água (elevação em relação ao solo)
#define TAP1_HEIGHT 2.10 //altura da torneira 1 == chuveiro
#define TAP2_HEIGHT 1.95 //altura da torneira 2 == chuveiro

class Control : public QThread
{
    Q_OBJECT

    public:

        bool tap1Open, tap2Open;

        bool tap1Closed, tap2Closed;

        bool wt1Full, wt2Full;



        bool pumpStatus;//bomba ligada = true, desligada = false
        double sisternCapacity;//capacidade da sisterna
        double pumpOutput;//vazão da bomba

        sem_t semaphorePump;//semáforo posix da bomba
        sem_t semaphoreWt1;//semáforo posix da caixa 1
        sem_t semaphoreWt2;//semáforo posix da caixa 2


        //Control(QTableWidget view);
        Control();

        //int Control_Main(void);
        int Control_Stop(void);

        void *Control_Pump_Control(void);
        void *Control_Wt1_Fill(void);
        void *Control_Wt2_Fill(void);
        void *Control_Wt1_Clear(void);
        void *Control_Wt2_Clear(void);

        //void Control_Print(void);

        void Control_SetSisternCapacity(double sisternCapacity);
        void Control_SetPumpOutput(double pumpOutput);
        void Control_SetTimeTap1(double totalTimeWt1Clear);
        void Control_SetTimeTap2(double totalTimeWt2Clear);

        void control_print_data();

        WaterTank *Control_GetWaterTank(int option);

        virtual ~Control();

    signals:
    void control_signal_print(QString  pumpStatus, QString semPump, QString semWt1,
                              QString semWt2, QString level1, QString leve2);

    protected:
        static void *Control_Pump_Control_Helper(void *pControl);

        static void *Control_Wt1_Fill_Helper(void *pControl);
        static void *Control_Wt1_Clear_Helper(void *pControl);

        static void *Control_Wt2_Fill_Helper(void *pControl);
        static void *Control_Wt2_Clear_Helper(void *pControl);

    private:

        void run();

        //QTableView *view;
        //QTableWidget *view;

        WaterTank *wt1;//caixa 1
        WaterTank *wt2;//caixa 2



        time_t lastTimeWt1Fill;
        time_t lastTimeWt2Fill;

        time_t lastTimeWt1Clear;
        time_t lastTimeWt2Clear;

        time_t totalTimeWt1Clear;
        time_t totalTimeWt2Clear;

        //double levelWt1, levelWt2;

        pthread_t pump_control;//thread da tarefa de controle do uso da bomba
        pthread_t wt1_fill;//thread da tarefa encher caixa 1
        pthread_t wt1_clean;//thread da tarefa esvaziar caixa 1
        pthread_t wt2_fill;//thread da tarefa encher caixa 2
        pthread_t wt2_clean;//thread da tarefa esvaziar caixa 2

};

#endif // CONTROL_H
